package com.titaniume.java;

import org.junit.Test;

import java.io.*;

/**
 * @program: JavaSenior
 * @description:
 *
 * 1.缓冲流：
 * BufferedInputStream
 * BufferedOutputStream
 * BufferedReader
 * BufferWriter
 *
 *
 *
 * 2.作用 提供流的读取、写入的速度
 *
 * 3.处理流， 套接在 已有流的基础上的
 *
 *
 *
 * @author: renfei.huang
 * @create: 2021-10-17 16:29
 */
public class BufferedTest {



    //BufferedInputStream、BufferedOutputStream 复制文件
    @Test
    public void BuffereStreamTest() throws IOException {

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        //1.造文件
        try {
            File srcFile = new File("彼岸图网.jpg");
            File destFile = new File("彼岸图网3.jpg");

            //2.造流
            //2.1 造节点流
             fileInputStream = new FileInputStream(srcFile);
             fileOutputStream = new FileOutputStream(destFile);

            //2.2 造缓冲流
             bufferedInputStream = new BufferedInputStream(fileInputStream);
             bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            //3.复制的细节 读取、写入
            byte [] buffer = new byte[10];
            int len;

            while ((len = bufferedInputStream.read(buffer))!= -1){
                bufferedOutputStream.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //4.关闭资源
            //要求 ： 先关闭外层的刘在关闭外层的流
            //关闭外层 流的同时 内层流也会自动关闭，关于内层流的关闭我们可以省略
            if (bufferedInputStream!= null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(bufferedOutputStream != null){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public  void copyFileWithBuffered(String srcPath,String destPath){

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        //1.造文件
        try {
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);

            //2.造流
            //2.1 造节点流
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);

            //2.2 造缓冲流
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            //3.复制的细节 读取、写入
            byte [] buffer = new byte[1024];
            int len;

            while ((len = bufferedInputStream.read(buffer))!= -1){
                bufferedOutputStream.write(buffer,0,len);
//                bufferedOutputStream.flush(); //刷新缓存区
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //4.关闭资源
            //要求 ： 先关闭外层的刘在关闭外层的流
            //关闭外层 流的同时 内层流也会自动关闭，关于内层流的关闭我们可以省略
            if (bufferedInputStream!= null){
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(bufferedOutputStream != null){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public  void testCopyFileWithBuffered(){

        long start = System.currentTimeMillis();
        String srcPath ="D:\\test\\001.mp4";
        String destPath = "D:\\test\\003.mp4";
        copyFileWithBuffered(srcPath,destPath);
        long end = System.currentTimeMillis();
        System.out.println("复制花费时间为："+ (end - start)); //复制花费时间为：269

    }


    /*
    * BufferedReader、BufferedWriter 实现文本文件的复制
    * */
    @Test
    public  void testBufferedReaderBufferedWriter() throws IOException {


        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            //创建文件和流
             bufferedReader = new BufferedReader(new FileReader(new File("dbcp.txt")));
             bufferedWriter = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));

             //读写操作
            //方式一：使用char[] 数组
           /* char [] cbuf = new char[1024];
            int len;

            while ((len = bufferedReader.read(cbuf))!= -1){
                bufferedWriter.write(cbuf,0,len);
            }*/


            //方式二：使用String
            String data;
            while ( (data = bufferedReader.readLine()) != null ){
                //data中不包含换行符号
                //方法一：
//                bufferedWriter.write(data +"\n");

                //方法二：
                bufferedWriter.write(data);
                bufferedReader.readLine(); //提供换行的操作

            }



        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //关闭资源
            if(bufferedReader!= null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(bufferedWriter != null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }

}
